﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace EvertonApp.AdminWeb
{
    public class XmlParser<T> where T : class, new()
    {
        private IEnumerable<String> _propiedades;
        private string _className;

        public XmlParser()
        {
            _propiedades = from item in typeof (T).GetProperties() select item.Name;
            _className = typeof (T).Name;
        }

        public IEnumerable<T> Parsear(String xml)
        {
            XElement elem = XElement.Parse(xml);

            IEnumerable<XElement> t = elem.Elements().Where(c => c.Name.LocalName == _className);

            var temporal = from item in elem.Elements().Where(c => c.Name.LocalName == _className)
                           select new T()
                                      {

                                      };
            





            //XElement elem = XElement.Parse(xml);

            //IEnumerable<XElement> t = elem.Elements().Where(c => c.Name.LocalName == "Comercio");

            //var temporal = from item in elem.Elements().Where(c => c.Name.LocalName == "Comercio")
            //               select new Comercio
            //               {
            //                   Direccion = (string)item.Elements().Where(c => c.Name.LocalName == "Direccion").
            //                       FirstOrDefault().Value,
            //                   Nombre = (string)item.Elements().Where(c => c.Name.LocalName ==
            //                       "Nombre").FirstOrDefault().Value,
            //                   IdComercio = new Guid(item.Elements().Where(c => c.Name.LocalName ==
            //                       "IdComercio").FirstOrDefault().Value),
            //                   Telefono = (string)item.Elements().Where(c => c.Name.LocalName ==
            //                       "Telefono").FirstOrDefault().Value
            //               };

            return null;
        }

        public IEnumerable<T> Obtener(XElement item)
        {
            var lista = new List<T>();

            foreach (var prop in _propiedades)
            {

            }

            return lista.AsEnumerable();
        }
    }
}